package xyz.xtt.asset.service.impl;

import java.time.LocalDateTime;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import xyz.xtt.asset.constants.AssetCache;
import xyz.xtt.asset.entity.TblArtAmberOrder;
import xyz.xtt.asset.mapper.TblArtAmberOrderMapper;
import xyz.xtt.asset.service.ITblArtAmberOrderService;
import xyz.xtt.common.starter.cache.CacheManager;

/**
 * <p>
 * 国家信息表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-01-19
 */
@Service
public class TblArtAmberOrderServiceImpl extends ServiceImpl<TblArtAmberOrderMapper, TblArtAmberOrder> implements ITblArtAmberOrderService {
	@Autowired
	private CacheManager cacheManager;

	/**
	 * @param transactionId
	 * @return
	 */
	@Override
	public TblArtAmberOrder info(Long transactionId) {
		TblArtAmberOrder order = new LambdaQueryChainWrapper<>(baseMapper).eq(TblArtAmberOrder::getOrderNumber, transactionId).one();
		if (Objects.nonNull(order)) {
			// 第三方响应字段内容较长，不缓存
			order.setThirdRequestResponse(StringUtils.EMPTY);
		}
		return order;
	}

	@Override
	public TblArtAmberOrder infoException(Long transactionId) {
		TblArtAmberOrder order = new LambdaQueryChainWrapper<>(baseMapper)
				.eq(TblArtAmberOrder::getOrderNumber, transactionId)
				.eq(TblArtAmberOrder::getThirdRequestStatus, 500)
				.one();
		return order;
	}

	@Override
	public TblArtAmberOrder infoWithCache(Long transactionId) {
		return cacheManager.cacheObj(() -> info(transactionId), AssetCache.TBL_ART_AMBER_ORDER, String.valueOf(transactionId));
	}

	@Override
	public Boolean updateArtAmberOrderStatus(String orderNumber, Long uid) {
		LambdaUpdateChainWrapper<TblArtAmberOrder> update = new LambdaUpdateChainWrapper<>(baseMapper);
		update.eq(TblArtAmberOrder::getOrderNumber, orderNumber);
		update.eq(TblArtAmberOrder::getAppUserId, uid);
		update.set(TblArtAmberOrder::getThirdRequestStatus, 200);
		return update.update();
	}

	@Override
	public Boolean updateArtAmberOrderStatusWithId(Long orderId) {
		LambdaUpdateChainWrapper<TblArtAmberOrder> update = new LambdaUpdateChainWrapper<>(baseMapper);
		update.eq(TblArtAmberOrder::getId, orderId);
		update.eq(TblArtAmberOrder::getThirdRequestStatus, 500);
		update.set(TblArtAmberOrder::getThirdRequestStatus, 200);
		update.set(TblArtAmberOrder::getCrt, LocalDateTime.now());
		return update.update();
	}
}
